<?php
namespace api\common\controllers;

use api\modules\v1\models\Login;
use Yii;
use frontend\models\PasswordResetRequestForm;
use frontend\models\ResetPasswordForm;
use frontend\models\SignupForm;
use frontend\models\ContactForm;
use yii\base\DynamicModel;
use zyj\ext\helpers\ArrayHelper;
use zyj\ext\response\ApiResponse;

/**
 * Site controller
 */
class BasicController extends ApiController
{
    public function authExcepts()
    {
        $parents =  parent::authExcepts(); // TODO: Change the autogenerated stub
        $res = ['smser'];
        return  array_merge($parents,$res);
    }

    /**
     *发送短信验证码接口
     * tag 1注册短信 2
     */
    public function actionSmser(){
        $code = rand(1111, 9999);
        $account = Yii::$app->request->post('account');
        $tag = Yii::$app->request->post('tag');
        $content = '验证码为：'.$code.'，30分钟之内有效，请尽快完成验证!【销售会】';
        $smser = Yii::$app->smser;
        $cache = Yii::$app->cache;
        $model = DynamicModel::validateData(compact('account','tag'),[
            ['account','trim'],
            ['account','required','message'=>'手机号不能为空'],
            ['account','string','max'=>11,'tooLong'=>'手机号不正确'],
            ['account','zyj\ext\validator\PhoneValidator'],
            ['tag','in','range'=>[1,2],'message'=>'类型参数']
        ]);
        if($model->hasErrors()){
            $first_error = ArrayHelper::myGetFirstError($model);
            return ApiResponse::showResult(101,[],$first_error);
        }else{
            if(!empty($tag)){
                $res = Login::isExistsPhone($model->account);
                if($tag == 1){

                    if($res){
                        return ApiResponse::showResult(101,[],'该手机号已注册');
                    }
                }else{
                    if(!$res){
                        return ApiResponse::showResult(101,[],'账号不存在');
                    }
                }
            }

            $data = $smser->sendSms($account, $content);
            if($data) {
                if(YII_DEBUG){
                    $time = 180000;
                }else{
                    $time = 1800;
                }
                $cache->set($account, $code, $time);
                return ApiResponse::showResult(200);
            } else {
                return ApiResponse::showResult(100);
            }
        }
    }

//    public function action
}
